package org.ccu.common;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectionManager {
	private static Connection connection;
	private static String url;
	private static String username;
	private static String password;

	static {
		try {
			
			Class.forName("oracle.jdbc.driver.OracleDriver");
			new ConnectionManager();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private ConnectionManager() {
		Properties ps = new Properties();
		InputStream is = ConnectionManager.class.getClassLoader()
				.getResourceAsStream("db.properties");
		

		try {
			ps.load(is);
			url = ps.getProperty("url");
			username = ps.getProperty("username");
			password = ps.getProperty("password");
			connection = DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	public static Connection getConnection() {
		if (connection != null) {
			return connection;

		}
		try {
			connection = DriverManager.getConnection(url,username,password);
			return connection;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static boolean close(Statement st,ResultSet rs) {
		
			try {
				rs.close();
				st.close();
				
				return true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		
	}

}
